﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CI;
using System.Data.SqlClient;
using System.Data;
namespace CI.Caja.DL
{
    public class DA_Comprobante: IDA_Comprobante
    {
        DA_Utilitario objUtility = new DA_Utilitario();
        public Int32 RegistrarComprobante(BE_Comprobante ComprobanteBE)
        {
            SqlConnection cnx = new SqlConnection(DA_Utilitario.ObtenerConexion());
            try
            {
                cnx.Open();
                SqlCommand cmd = new SqlCommand("SP_INS_COMPROBANTE", cnx);
                cmd.CommandType = CommandType.StoredProcedure;

                var _with1 = (cmd.Parameters);
                _with1.Add("@Serie", SqlDbType.VarChar).Value = ComprobanteBE.Serie;
                _with1.Add("@Numero", SqlDbType.VarChar).Value = ComprobanteBE.Numero;
                _with1.Add("@IdPaciente", SqlDbType.VarChar).Value = ComprobanteBE.IdPaciente;
                _with1.Add("@Nombre", SqlDbType.VarChar).Value = ComprobanteBE.Nombre;
                _with1.Add("@NroOrden", SqlDbType.VarChar).Value = ComprobanteBE.NroOrden;
                _with1.Add("@IdMedioPago", SqlDbType.VarChar).Value = ComprobanteBE.IdMedioPago;
                _with1.Add("@IdModalidadPago", SqlDbType.VarChar).Value = ComprobanteBE.IdModalidadPago;
                _with1.Add("@ImporteIgv", SqlDbType.VarChar).Value = ComprobanteBE.ImporteIgv;
                _with1.Add("@ImporteTotal", SqlDbType.VarChar).Value = ComprobanteBE.ImporteTotal;

                return cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                cnx.Close();
            }
        }


        public string ObtenerImporteTotalIngresos()
        {
            string importe = "";
            string sentencia = "SELECT ISNULL(sum(numImporteTotal),0) as numImporteTotal  from CA_COMPROBANTE WHERE DATEDIFF(d,@fecha,getdate())=0";
           // using (SqlConnection conexion = new SqlConnection(DA_Utilitario.ObtenerConexion()))
            using (SqlConnection conexion = DA_SingletonConexion.ConnectionInstance)
            {
                conexion.Open();

                 try
               {
                using (SqlCommand comando = new SqlCommand(sentencia, conexion))
                {
                    comando.Parameters.Add(new SqlParameter("@fecha", DateTime.Now));

                    SqlDataReader resultado = comando.ExecuteReader();
                    if (resultado.Read())
                    {
                        importe = resultado["numImporteTotal"].ToString();

                    }
                }


               }
                 catch (SqlException error)
                 {
                     throw new Exception(error.Message);
                 }
                 finally
                 {
                     if (conexion != null)
                     {
                         if (conexion.State == System.Data.ConnectionState.Open) conexion.Close();
                     }
                 }
            }
            return importe;
        }
    }
}
